package com.example.liu_test.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import org.apache.catalina.connector.Request;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CookieValue;


import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

/***
 * 自定义字段自动填充类
 * @author lw
 * @date 2021/07/08 上午
 *
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

     /*
      *新增数据时自动写入
      */
    @Override
    public void insertFill(MetaObject metaObject) {


        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(TokenUtil.getTokenUserId());
        this.setFieldValByName("createTime", df.format(new Date()), metaObject);
        this.setFieldValByName("updateTime", df.format(new Date()), metaObject);
        this.setFieldValByName("createName", TokenUtil.getTokenUserId(), metaObject);
        this.setFieldValByName("updateName", TokenUtil.getTokenUserId(), metaObject);
        this.setFieldValByName("dr", "0", metaObject);
    }
    /*
     *更新数据时，自动写入
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setFieldValByName("updateTime", df.format(new Date()), metaObject);
        this.setFieldValByName("updateName", TokenUtil.getTokenUserId(), metaObject);
    }

}
